package com.common.security.abstracts;

import com.common.security.interfaces.ISecurity;

/**
 * Errors are security problems that arise and will be logged and saved in the database.
 * They do not offer a way to recover from because they are not runtime exceptions.
 * 
 * Contain:
 * String message - the error message
 * String cause - the cause that created the error
 * String description - description given by the system for the problem
 * int code - error code 
 * 
 * Default Error code range: 1 - 20
 * 
 * @author Ady
 *
 */
public abstract class AbstractError implements ISecurity {
	
	public static final int INVALID_REQUEST = 1;
	private static final String MESSAGE_INVALID_REQUEST = "The request was invalid for this action";
	private static final String CAUSE_INVALID_REQUEST= "The request is not appropriate for this action";
	private static final String DESCRIPTION_INVALID_REQUEST = "Make sure the request and the action match";
	
	public static final int DATABASE_ERROR = 2;
	private static final String MESSAGE_DATABASE_ERROR = "The system had an error";
	private static final String CAUSE_DATABASE_ERROR = "The system had an error";
	private static final String DESCRIPTION_DATABASE_ERROR = "The system will try to recover from the error, please try again and if the error persist contact the administrator";
	
	public static final int UNKNOWN_ERROR = 3;
	private static final String MESSAGE_UNKNOWN_ERROR = "The system had an unknown error";
	private static final String CAUSE_UNKNOWN_ERROR = "The reason for this error is unknown";
	private static final String DESCRIPTION_UNKNOWN_ERROR = "Please contact the administrator related to this problem";
	
	private String message;
	private String cause;
	private String description;
	private int code;
	
	public AbstractError() {
		this.message = "";
		this.cause = "";
		this.description = "";
	}
	
	public AbstractError(int code) {
		this.code = code;
		
		switch (code) {
			case INVALID_REQUEST: {
				setMessage(MESSAGE_INVALID_REQUEST);
				setCause(CAUSE_INVALID_REQUEST);
				setDescription(DESCRIPTION_INVALID_REQUEST);
			}
			case DATABASE_ERROR: {
				setMessage(MESSAGE_DATABASE_ERROR);
				setCause(CAUSE_DATABASE_ERROR);
				setDescription(DESCRIPTION_DATABASE_ERROR);
				break;
			}
			case UNKNOWN_ERROR: {
				setMessage(MESSAGE_UNKNOWN_ERROR);
				setCause(CAUSE_UNKNOWN_ERROR);
				setDescription(DESCRIPTION_UNKNOWN_ERROR);
				break;
			}
			default: {
				setMessage(MESSAGE_UNKNOWN_ERROR);
				setCause(CAUSE_UNKNOWN_ERROR);
				setDescription(DESCRIPTION_UNKNOWN_ERROR);
				break;
			}
		}
	}
	
	public String getMessage() {
		return message;
	}
	
	public void setMessage(String message) {
		this.message = message;
	}

	public String getCause() {
		return cause;
	}

	public void setCause(String cause) {
		this.cause = cause;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public int getCode() {
		return code;
	}
	
	public void setCode(int code) {
		this.code = code;
	}
}
